<!doctype html>
<html >
<head>
	<meta charset="UTF-8">
	<title>新建文件-RPA机器人工作台</title>
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
	<link rel="stylesheet" href="/rpa_work/static/css/xadmin.css?v=1.01">
    <script type="text/javascript" src="/rpa_work/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="/rpa_work/static/js/jquery.cookie.js"></script>
    <script src="/rpa_work/static/layui/layui.js?v=2.6.8" charset="utf-8"></script>
    <script>var page="addfile";</script>
	<script type="text/javascript" src="/rpa_work/static/js/xadmin.js?v=1.0"></script>
	<script type="text/javascript" charset="utf-8" src="/rpa_work/static/js/xm-select.js"></script>
    <script type="text/javascript" src="/rpa_work/static/js/target_window.js"></script>
</head>
    <body>
        <div class="x-body">
        
		<div class="layui-card">
		  <div class="layui-card-body">
		    	<div class="layui-row">
					<form class="layui-form layui-form-pane" method="post" id="myform" lay-filter="myform">
                      <div class="layui-form-item">
                        <label class="layui-form-label">任务名称</label>
                        <div class="layui-input-block">
                          <select name="flow_id" id="taskchange" lay-filter="taskchange" lay-search>
                          <option value="">请选择</option>
                        </select>
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">上传文件</label>
                        <div class="layui-input-inline" style="width:305px">
                          <input type="text" name="file_name" id="file_name" required lay-verify="required" readonly placeholder="点击上传文件，自动获取" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline" style="width:95px">
                            <button class="layui-btn layui-btn-primary" id="uploadfile" type="button">上传文件</button>
                            <input class="layui-upload-file" type="file" name="file">
                        </div>
                      </div>
   <div class="layui-form-item" id="up-progress" style=" display:none;">                   
<div class="layui-progress" lay-showPercent="true" lay-filter="up-progress" >
<div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
</div>
</div>
					  <div class="layui-form-item savebtn" style="text-align:center">
                        <button type="button" class="layui-btn"  id="saveform">保存</button>
                      </div>
					</form>
                    
				</div>

		  </div>
		</div>
      </div>


		<script>
	    layui.use(['form','layer','upload'], function(){
                $ = layui.jquery;
              var layer = layui.layer;
			  var form = layui.form;
			  var upload = layui.upload;
			  var flowid;

								  $.ajax({
									url: apiurl+"Quest_list",
									type: 'POST',
									dataType: 'json',
									data: JSON.stringify({'sid':sid}),
									headers: {
										'Content-Type': 'application/json'
									},
									success: function(res) {
                                         if(res.status==0){
                                            $('#taskchange').empty();
                                            //$('#taskchange').append('<option value="">请选择</option>');
                                            $.each(res.data, function(index, flow) {
                                                    if(index==0)flowid=flow.flow_id
                                                    var $option = $('<option></option>');
                                                    $option.val(flow.flow_id);
                                                    $option.text(flow.flow_name);

                                                    $('#taskchange').append($option);
                                            });
                                            form.render('select', 'myform');
                                         }else{
                                            //layer.msg(res.msg);
                                            layer.confirm('获取不到任务名称', {
                                              btn: ['返回列表','重试']
                                              ,closeBtn: 0
                                            }, function(){
                                               parent.location.reload();
                                            }, function(){
                                               window.location.href=location.href;
                                            });
                                         }
									},
									error: function(xhr, status, error) {
										layer.msg(error);
									}
								});
				form.on('select(taskchange)', function(data){
				  flowid=data.value;
				});
					  //执行实例
					  var uploadInst = upload.render({
						elem: "#uploadfile"
						,url: apiurl+"FileUpload"
						//,headers: {'Content-Type': 'application/json'}
						,accept:"file"
						,auto: false
						,bindAction: '#saveform'
						  ,choose: function(obj){
							obj.preview(function(index, file, result){
							  $('#file_name' ).val(file.name);
							});
							$('#up-progress' ).show();
							element.progress('up-progress', 0);
						  }
						  ,before: function(obj){
							if(flowid=='' || flowid ===undefined){
								layer.msg('请先选择任务');
								return false;
							}
							var that = this;
							layer.load();
							var file_name;						
							var files = obj.pushFile();
							$.each(files, function(index, file) {
								//$('#file_name').val(file.name);
								file_name=file.name;
							});
							console.log(file_name);
							  that.data = {
								sid: sid,
								'file_name': file_name,
								'flow_id':flowid
							  };
						  }
						,progress: function(n, elem) {
							var percent = n + '%' //获取进度百分比
							element.progress('up-progress', percent);
						}
						,done: function(res){
							layer.closeAll('loading');
							if(res.status==0){
								layer.confirm('添加成功', {
								  btn: ['返回列表','继续']
								  ,closeBtn: 0
								}, function(){
								   parent.location.reload();
								}, function(){
								   window.location.href=location.href;
								});
							}else{
								checklogin(res);
								 layer.alert(res.msg, {icon: 5});
							}
						}
						,error: function(){
						  //请求异常回调
						  layer.alert("上传异常！");
						}
					  });
              
            });
	
       </script>
    </body>
</html>